﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using TimKen.DataEntity;
using TimKen.BussLogic;
using TimKen.App;

namespace TimKen.Web.Admin.Order
{
    public partial class Order_List : TimKen.App.AdminPage
    {
        OrderManager orderM = new OrderManager();
        public override string ActionCode
        {
            get
            {
                return "86";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindOrderStatus();
                BindResources();
                BindData();
            }
        }

        protected bool Direction
        {
            get
            {
                if (ViewState["m_SortDirection"] == null)
                {
                    return true;
                }
                return (bool)ViewState["m_SortDirection"];
            }
            set { ViewState["m_SortDirection"] = value; }
        }

        protected string Expression
        {
            get { return (string)ViewState["m_SortExpression"]; }
            set { ViewState["m_SortExpression"] = value; }
        }


        protected void BindOrderStatus()
        {
            ddlOrderStatus.Items.Add(new ListItem(GetLangString("All"), "-1"));
            ddlOrderStatus.DataTextField = "Status";
            ddlOrderStatus.DataValueField = "ID";
            ddlOrderStatus.DataSource = orderM.GetOrderStatus(this.LanguageID);
            ddlOrderStatus.DataBind();
        }

        public void BindResources()
        {
            btnSearch.Text = GetLangString("Search");
            grdOrderList.Columns[1].HeaderText = GetLangString("OrderCreator");
            grdOrderList.Columns[2].HeaderText = GetLangString("OrderDate");
            grdOrderList.Columns[3].HeaderText = GetLangString("TotalAmount");
            grdOrderList.Columns[4].HeaderText = GetLangString("TaxAmount");
            grdOrderList.Columns[5].HeaderText = GetLangString("CardCode");
            grdOrderList.Columns[6].HeaderText = GetLangString("ExpectDate");
            grdOrderList.Columns[7].HeaderText = GetLangString("OrderStatus");
        }

        protected void BindData()
        {
            string where = "1=1";

            if (!string.IsNullOrEmpty(this.txtOrderNo.Text.Trim()))
                where += " AND OrderNo LIKE '%" + this.txtOrderNo.Text.Trim() + "%'";

            if (!string.IsNullOrEmpty(this.txtCreateDateBegin.Text.Trim()))
                where += " AND  CreateDate >='" + Convert.ToDateTime(this.txtCreateDateBegin.Text.Trim()).ToString() + "'";

            if (!string.IsNullOrEmpty(this.txtCreateDateEnd.Text.Trim()))
                where += " AND  CreateDate <='" + Convert.ToDateTime(this.txtCreateDateEnd.Text.Trim()).ToString() + "'";

            if (this.ddlOrderStatus.SelectedIndex != 0)
                where += " AND  OrderStatus = " + this.ddlOrderStatus.SelectedValue;


            where += " AND  OrderType=" + (int)OrderType.Order;

            DataSet ds = orderM.GetList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, where, Expression, Direction, this.LanguageID);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);
            this.grdOrderList.DataSource = ds;
            this.grdOrderList.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 0;
            BindData();
        }

        protected void grdOrderList_Sorting(object sender, GridViewSortEventArgs e)
        {
            Direction = !Direction;
            Expression = e.SortExpression;

            BindData();
        }


        protected void ShowSortedIcon(GridView gvData, GridViewRow gvRow)
        {
            for (int index = 0; index <= gvData.Columns.Count - 1; index++)
            {
                if ((gvData.Columns[index].SortExpression == Expression) && (gvData.Columns[index].SortExpression != ""))
                {
                    Image img = new Image();
                    if (Direction)
                    {
                        img.ImageUrl = "../images/sort_asc.gif";
                        img.ToolTip = GetLangString("ASCSort");
                    }
                    else
                    {
                        img.ImageUrl = "../images/sort_desc.gif";
                        img.ToolTip = GetLangString("DESCSort");
                    }
                    gvRow.Cells[index].Controls.Add(img);
                }
            }
        }

        protected void grdOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                ShowSortedIcon(this.grdOrderList, e.Row);
            }
        }

        protected void ddOrderStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }



    }
}
